package com.suning.sawp.service.impl.bi;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.intf.ZoneChangeService;

/**
 * 
 * 系统管理服务类<br>
 * 〈功能详细描述〉
 *
 * @author 14062606
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class ZoneChangeServiceImpl implements ZoneChangeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ZoneChangeServiceImpl.class);

    @Resource
    DalClient dalClient;

    @Transactional
    @Override
    public ReturnMsg<String> updateZoneCode(String orgZoneCode, String currZoneCode) {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        if (StringUtils.isBlank(currZoneCode) || StringUtils.isBlank(orgZoneCode)) {
            LOGGER.error(" ZoneChangeServiceImpl updateZoneCode input is null orgZoneCode:" + orgZoneCode
                    + ", currZoneCode:" + currZoneCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
            return returnMsg;
        }
        
        if(!checkOrgZoneCode(orgZoneCode)){
            LOGGER.error(" ZoneChangeServiceImpl checkOrgZoneCode fali orgZoneCode:" + orgZoneCode);
            returnMsg.setError(ErrorCodeConstants.ERR_0001, "HR数据中该大区仍有门店存在，请先联系HR修改门店数据再合并");
            return returnMsg;
        }

        if(orgZoneCode.length() == 5){
            updateModelAnalyse(orgZoneCode, currZoneCode);
            updateSaleAnalyse(orgZoneCode, currZoneCode);
            updateModelSaleTop(orgZoneCode, currZoneCode);
            updateModelAnalyseIn(orgZoneCode, currZoneCode);
        }else   if(orgZoneCode.length() == 8){
            updateModeMainList(orgZoneCode, currZoneCode);
            updateModelProblemList(orgZoneCode, currZoneCode);
            updateModelValidity(orgZoneCode, currZoneCode);
        }
        
        return returnMsg;
    }

    /**
     * 更新样机清单 大区信息
     * 样机清单 MODEL_MAIN_LIST
     *
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private boolean checkOrgZoneCode(String orgZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        try {
            long result  = dalClient.queryForObject("ZONE_CHANGE.CHECK_ORGZONECODE", paramMap, Long.class);
            if(result > 0){
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("checkOrgZoneCode error,orgZoneCode : "+ orgZoneCode, e);
            return false;
        }
        return true;
    }
    
    /**
     * 更新样机清单 大区信息
     * 样机清单 MODEL_MAIN_LIST
     *
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModeMainList(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_MAIN_LIST", paramMap);
        LOGGER.info("updateModeMainList result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }
    
    /**
     * 更新样机异常以及审核数据 大区信息
     * 样机异常以及审核数据 MODEL_PROBLEM_LIST
     *ModelProblemList
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModelProblemList(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_PROBLEM_LIST", paramMap);
        LOGGER.info("updateModelProblemList result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }        
    

    /**
     * 更新样机有效性 大区信息
     * 样机有效性 MODEL_VALIDITY
     *ModelValidity
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModelValidity(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_VALIDITY", paramMap);
        LOGGER.info("updateModelValidity result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }       
    
    /**
     * 更新样机分析 大区信息
     * 样机分析 MODEL_ANALYSE
     *ModelAnalyse
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModelAnalyse(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_ANALYSE", paramMap);
        LOGGER.info("updateModelAnalyse result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }        
    
    /**
     * 更新销售分析 大区信息
     * 销售分析 SALE_ANALYSE
     *SaleAnalyse
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateSaleAnalyse(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);

        int result = dalClient.execute("ZONE_CHANGE.UPDATE_SALE_ANALYSE", paramMap);
        LOGGER.info("updateSaleAnalyse result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
    
        return;
    }       
    
    /**
     * 更新样机TOP 大区信息
     * 样机TOP MODEL_SALE_TOP
     *ModelSaleTop
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModelSaleTop(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_SALE_TOP", paramMap);
        LOGGER.info("updateModelSaleTop result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }     
    
    /**
     * 更新后台样机分析 大区信息
     * 后台样机分析 MODEL_ANALYSE_IN
     *ModelAnalyseIn
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void updateModelAnalyseIn(String orgZoneCode, String currZoneCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orgZoneCode", orgZoneCode);
        paramMap.put("currZoneCode", currZoneCode);
        int result = dalClient.execute("ZONE_CHANGE.UPDATE_MODEL_ANALYSE_IN", paramMap);
        LOGGER.info("updateModelAnalyseIn result:{},orgZoneCode:{}, currZoneCode:{}", result, orgZoneCode,
                currZoneCode);
        return;
    }    
    
}
